#!/bin/bash

############################################################################
# Copyright Nash!Com, Daniel Nashed 2019, 2020 - APACHE 2.0 see LICENSE
# Copyright IBM Corporation 2015, 2019 - APACHE 2.0 see LICENSE
############################################################################

SCRIPT_NAME=$0
PARAM1=$1

SOFTWARE_DIR=/local/software
HASHFILE=/local/software/software_dir_sha256.txt
SOFTWARE_FILE=/local/software/software.txt

# need full path because of cd later in script logic
FULL_SCRIPT_NAME=`realpath $0`
FULLHASHFILE=`realpath $HASHFILE`
FULL_SOFTWARE_FILE=`realpath $SOFTWARE_FILE`

usage ()
{
  echo
  echo "Usage: `basename $SCRIPT_NAME` { check | calc | software}"

  return 0
}

check_hashes ()
{
  if [ ! -r "$FULLHASHFILE" ]; then
    echo "Cannot read checksum file [$HASHFILE]!"
    return 1
  fi

  cd $SOFTWARE_DIR
  sha256sum -c $FULLHASHFILE
  
  return 0
}

get_download_name ()
{
  DOWNLOAD_NAME=""
  if [ -e "$FULL_SOFTWARE_FILE" ]; then
    DOWNLOAD_NAME=`grep "$1|$2|" "$FULL_SOFTWARE_FILE" | cut -d"|" -f3`
  fi

  return 0
}

test_install_software ()
{
  PROD_NAME=DOMINO
  PROD_VER=10.0.1
  PROD_FP=FP1
  PROD_HF=IF1

  INST_VER=$PROD_VER

  if [ -z "$PROD_FP" ]; then
    INST_FP=""
  else
    INST_FP=$PROD_VER$PROD_FP
  fi

  if [ -z "$PROD_HF" ]; then
    INST_HF=""
  else
    INST_HF=$PROD_VER$PROD_FP$PROD_HF
  fi

  echo $PROD_NAME

  get_download_name $PROD_NAME $INST_VER
  echo "$INST_VER -> [$DOWNLOAD_NAME]"

  get_download_name $PROD_NAME $INST_FP
  echo "$INST_FP -> [$DOWNLOAD_NAME]"

  get_download_name $PROD_NAME $INST_HF
  echo "$INST_HF -> [$DOWNLOAD_NAME]"

  return 0
}

update_software_list ()
{

  if [ ! -r "$FULLHASHFILE" ]; then
    echo "Cannot read checksum file [$HASHFILE]!"
    return 1
  fi

  while read line; do    
    HASH=`echo $line | cut -d" " -f1`
    FILENAME=`echo $line | cut -d"*" -f2`
    
    if [ -e "$FULL_SOFTWARE_FILE" ]; then
      DOWNLOAD_NAME=`grep "|$HASH" "$FULL_SOFTWARE_FILE" | grep "|$FILENAME|" | cut -d"|" -f3`
    else
      DOWNLOAD_NAME=""
    fi

    if [ -z "$DOWNLOAD_NAME" ]; then
      echo "PRODUCT|VERSION|$FILENAME|PARTNO|$HASH" >> $FULL_SOFTWARE_FILE
      echo "Adding $FILENAME"
    fi

  done < $FULLHASHFILE

  return 0
}

calc_hashes ()
{
  if [ -e "$FULLHASHFILE" ]; then
    rm -f "$FULLHASHFILE"
  fi

  cd $SOFTWARE_DIR
  sha256sum -b *.tar >> $FULLHASHFILE 2>/dev/null
  sha256sum -b *.taz >> $FULLHASHFILE 2>/dev/null
  sha256sum -b *.tar.gz >> $FULLHASHFILE 2>/dev/null

  if [ -e "$FULLHASHFILE" ]; then
    echo "Calc hash for `cat $FULLHASHFILE |wc -l` file(s) [`date -d@$SECONDS -u +%T`]"
  else
    echo "No Files found to calculate hash"
  fi

  return 0
}


echo

case "$PARAM1" in
	
  test)
    test_install_software
    ;;

  check)
    check_hashes 
    ;;

  calc)
    calc_hashes
    ;;

  software)
    update_software_list
    ;;

  help|-?)
    usage
    ;;

  *)
    if [ -z "$PARAM1" ]; then
      usage 
    else
      echo "Invalid command:" [$PARAM1]
      usage 
    fi
    ;;

esac

echo 
exit 0
